{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### IO 操作\n",
    "- save\n",
    "- savez \n",
    "- load\n",
    "\n",
    "- savetxt\n",
    "- loadtxt"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### numpy.savez(file, *args, **kwds)\n",
    "- file\t要保存的文件，扩展名为 .npz，如果文件路径末尾没有扩展名 .npz，该扩展名会被自动加上\n",
    "- args\t要保存的数组\n",
    "- kwds\t要保存的数组使用关键字名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "b = np.array([11, 22, 33, 44, 55, 66]) \n",
    "c = np.array([10, 20, 30, 40, 50, 60]) \n",
    "np.savez('outfile_abc', b, c,arr_load = c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<numpy.lib.npyio.NpzFile object at 0x00000171593489D0>\n",
      "b: [11 22 33 44 55 66]\n",
      "c: [10 20 30 40 50 60]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([10, 20, 30, 40, 50, 60])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_re = np.load('outfile_abc.npz')\n",
    "print(a_re)\n",
    "\n",
    "print(\"b:\",a_re['arr_0'])\n",
    "print(\"c:\",a_re['arr_1'])\n",
    "\n",
    "a_re['arr_load']"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### np.savetxt(file, arr, fmt=\"%d\", delimiter=\",\")\n",
    "- file\t要保存的文件\n",
    "- arr\t要保存的数组\n",
    "- fmt\t指定数组元素的保存格式\n",
    "- delimiter\t指定每行元素之间的分隔符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.arange(16).reshape(4,4)\n",
    "np.savetxt('outfile_2d.txt', arr, fmt='%d', delimiter=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.loadtxt('outfile_2d.txt', dtype=np.int32, delimiter=',')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.8"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "6fff98fc3b3d81bd655c2cc48858186e4d9e2db7b515bf1c3221888f12a62f87"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
